﻿using PPSMS_Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PPSMS_DAL;

public class StationStatus_DAL : ICRUD_DAL<StationStatus, long>
{
    /// <summary>
    /// 删除指定ID的记录
    /// </summary>
    /// <param name="ids">电站状态表的ID数组</param>
    public void DeleteByIds(params long[] ids)
    {
        string sql = $"delete from STATION_STATUS where Id in ({string.Join(",", ids)})";
        DBHelper.ExecuteNonQuery(sql);
    }

    /// <summary>
    /// 获取当前最大ID值
    /// </summary>
    /// <returns>ID</returns>
    public long GetCurrentValue()
    {
        string sql = "select rowid from STATION_STATUS";
        return DBHelper.ExecuteQuery(sql, DBHelper.ExecuteScalarToInt, new object[0]);
    }

    /// <summary>
    /// 新增记录
    /// </summary>
    /// <param name="t">电站状态表对象</param>
    public void Insert(StationStatus t)
    {
        string sql = "insert into STATION_STATUS (TIMESTAMP, CURRENT_POWER, AVERAGE_POWER, INVERTER_COUNT) values (?,?,?,?)";
        DBHelper.ExecuteNonQuery(sql, t.Timestamp, t.CurrentPower, t.AveragePower, t.InverterCount);
    }

    /// <summary>
    /// 将一个 DataRow 对象转换为对应的 StationStatus 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有电站状态表信息的List</returns>
    public StationStatus ResultHandler(DataRow dr)
    {
        return new StationStatus()
        {
            Id = dr.Field<long>("ID"),
            Timestamp = dr.Field<string>("TIMESTAMP"),
            CurrentPower = dr.Field<double>("CURRENT_POWER"),
            AveragePower = dr.Field<double>("AVERAGE_POWER"),
            InverterCount = dr.Field<long>("INVERTER_COUNT")
        };
    }

    /// <summary>
    /// 将一个 DataTable 对象转换为对应的 List<StationStatus> 对象
    /// </summary>
    /// <param name="dr"></param>
    /// <returns>一个包含所有电站状态表信息的List</returns>
    public List<StationStatus> ResultHandlerList(DataTable dr)
    {
        List<StationStatus> list = new List<StationStatus>();
        foreach (DataRow item in dr.Rows)
        {
            list.Add(ResultHandler(item));
        }
        return list;
    }

    /// <summary>
    /// 获取所有电站状态表的记录
    /// </summary>
    /// <returns>一个包含所有电站状态表信息的List</returns>
    public List<StationStatus> SelectAll()
    {
        string sql = "select * from STATION_STATUS";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 根据ID获取电站状态表的记录
    /// </summary>
    /// <param name="id"></param>
    /// <returns>电站状态表对象</returns>
    public StationStatus? SelectById(long id)
    {
        return SelectAll().FirstOrDefault(x => x.Id == id);
    }

    /// <summary>
    /// 根据条件修改电站状态表的记录
    /// </summary>
    /// <param name="t">新的电站状态表对象</param>
    /// <param name="id">旧的ID</param>
    /// <exception cref="ArgumentException"></exception>
    public void UpdateById(StationStatus t, long id)
    {
        string sql = "update STATION_STATUS set ";
        var list = new List<object>();
        if (t.Timestamp != null)
        {
            sql += "TIMESTAMP = ?,";
            list.Add(t.Timestamp);
        }
        if (t.CurrentPower != null)
        {
            sql += "CURRENT_POWER = ?,";
            list.Add(t.CurrentPower);
        }
        if (t.AveragePower != null)
        {
            sql += "AVERAGE_POWER = ?,";
            list.Add(t.AveragePower);
        }
        if (t.InverterCount != null)
        {
            sql += "INVERTER_COUNT = ?,";
            list.Add(t.InverterCount);
        }
        sql = sql.TrimEnd(',');
        if (list.Count == 0) throw new ArgumentException("没有任何属性需要更新");
        sql += " where ID = ?";
        list.Add(id);
        DBHelper.ExecuteNonQuery(sql, list.ToArray());
    }

    /// <summary>
    /// 查询最新一条记录
    /// </summary>
    /// <returns></returns>
    public List<StationStatus> SelectNewest()
    {
        string sql = "SELECT * FROM STATION_STATUS ORDER BY ID DESC LIMIT 1;";
        return DBHelper.ExecuteQuery(sql, ResultHandler, new object[0]);
    }

    /// <summary>
    /// 修改逆变器数量
    /// </summary>
    /// <param name="tag">tag等于true为加，tag等于false为减</param>
    public void UpdateNewest(bool tag)
    {
        var stations = SelectNewest();
        var station = new StationStatus();
        foreach (var stat in stations)
        {
            station = stat;
        }
        if (tag)
        {
            station.InverterCount += 1;
        }
        else
        {
            station.InverterCount -= 1;
        }
        UpdateById(station, station.Id);
    }
}
